package cn.huixh.blindbox.controller;

import cn.huixh.blindbox.annotation.TokenVerify;
import cn.huixh.blindbox.common.Common;
import cn.huixh.blindbox.common.ResultData;
import cn.huixh.blindbox.pojo.dto.NoteDTO;
import cn.huixh.blindbox.pojo.vo.NoteVO;
import cn.huixh.blindbox.service.BoxService;
import cn.huixh.blindbox.service.TokenService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 盲盒接口
 *
 * @author huixh
 * @version 1.0
 * @date 2022/5/9 14:37
 */
@RestController
@RequestMapping("/box")
@RequiredArgsConstructor
public class BoxController {

    final BoxService boxService;

    final TokenService tokenService;

    /**
     * 放入盲盒
     *
     * @param note 纸条实体对象
     * @return 操作结果
     */
    @TokenVerify
    @PutMapping("/put")
    public ResultData< String > putNote(@RequestHeader(Common.TOKEN_KEY) String token, @Validated @RequestBody NoteDTO note) {
        boxService.putNote(token, note);
        return ResultData.success("放置成功！");
    }

    /**
     * 随机获取
     *
     * @param sex 性别
     * @return 操作结果
     */
    @TokenVerify
    @GetMapping("/get/{sex}")
    public ResultData< NoteVO > getNote(@PathVariable String sex) {
        NoteVO note = boxService.getRandomNote(sex);
        return ResultData.success(note);
    }

    /**
     * 获取自身 文章
     *
     * @param token token
     * @return list
     */
    @GetMapping("/get/self")
    public ResultData< List< NoteVO > > getThisNotes(@RequestHeader(Common.TOKEN_KEY) String token) {
        String accountId = tokenService.getAccountId(token);
        List< NoteVO > voList = boxService.getSelf(accountId);
        return ResultData.success(voList);
    }

}
